// pages/index3/index3.js
var wxParse = require('../../wxParse/wxParse.js');
var appData = getApp().globalData;
var common = require("../../utils/Conmon.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    viewmodel: true,
    ruesmodel: true,
    shoumodel: true,
    successmodel: true,
    errormodel: true,
    hideIDCard: true,
    hideIDCardAdd: false,
    hideBusinesslicense: true,
    hideBusinesslicenseAdd: false,
    UserName: "",
    Phone: "",
    ShopName: "",
    IDCard: "",
    BusinessLicense: "",
    Agreement: "",
    hideUserName: true,
    hidePhone: true,
    hideShopName: true,
    hideIDCardDIV: true,
    hideBusinesslicenseDIV: true,
    popupWaitModel: true,
    second: 60,
    ButtonDisabled: false,
    ButtonText: "提交入驻申请",
    popupPassedMode: true,
    popupWaitPayModel: true,
    popupRefuseMode: true,
    CheckRemark: "",
    popupNoPowerMode: true,
    noPowerMsg: "",
    OrderNo:""
  },
  //审核已受理
  errorModel: function () {
    this.setData({
      errormodel: !(this.data.errormodel),
    })
  },
  //审核已通过
  successModel: function () {
    this.setData({
      successmodel: !(this.data.successmodel),
    })
  },
  //审核已受理
  shouModel: function () {
    this.setData({
      shoumodel: !(this.data.shoumodel),
    })
  },

  //审核未通过
  viewModel: function () {
    this.setData({
      viewmodel: !(this.data.viewmodel),
    })
  },
  // 协议
  ruesModel: function () {
    this.setData({
      ruesmodel: !(this.data.ruesmodel),
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadAgreement();
    this.loadSetting();
    this.loadApply();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }//上传文件
  ,
  upload_file: function (url, filePath, name, success, fail) {
    wx.uploadFile({
      url: url,
      filePath: filePath,
      name: name,
      formData: {
        'customerid': appData.custID,
        'userid': wx.getStorageSync('UserInfo').UserID,
        'imageType': 0,
        'imageSize': 0,
        'isWeixin': 0
      },// HTTP 请求中其他额外的参数
      header: {
        'content-type': 'multipart/form-data'
      }, // 设置请求的 header
      success: function (res) {
        if (res.statusCode == 200 && !res.data.result_code) {
          typeof success == "function" && success(res.data);
        } else {
          typeof fail == "function" && fail(res);
        }
      },
      fail: function (res) {

        typeof fail == "function" && fail(res);
      }
    })
  },
  /**
   * 上传凭证
   */
  onUploadPics: function (imgType) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {


        that.upload_file('http://img4.winmobi.cn/Baidu/Upload', res.tempFilePaths[0], 'photo',
          function (res) {
            //调用上传文件成功
            //console.log(res);
            var resData = JSON.parse(res)
            if (imgType == "IDCard") {
              that.setData({
                IDCard: resData.dataUrl,
                hideIDCard: false,
                hideIDCardAdd: true
              });
            } else if (imgType == "Businesslicense") {
              that.setData({
                BusinessLicense: resData.dataUrl,
                hideBusinesslicense: false,
                hideBusinesslicenseAdd: true
              });
            }


          });
      }
    });
  },
  UploadIDCard: function () {
    var that = this;
    that.onUploadPics("IDCard");

  },
  UploadBusinesslicense: function () {
    var that = this;
    that.onUploadPics("Businesslicense");
  },
  deletePicIDCard: function () {
    var that = this;
    that.setData({
      hideIDCard: true,
      hideIDCardAdd: false
    });
  },
  deletePicBusinessLicense: function () {
    var that = this;
    that.setData({
      hideBusinesslicense: true,
      hideBusinesslicenseAdd: false
    });
  },
  loadAgreement: function () {
    var that = this;
    wx.request({
      url: appData.apiurl + "/mobile/api/Unionstore/GetAgreement",
      data: { "custID": appData.custID },
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        var data = res.data.content;
        if (data.Agreement) {
          wxParse.wxParse('Agreement', 'html', data.Agreement, that, 5)
        }


      },
      fail: function () {

        wx.showToast({
          title: '获取协议失败',
          duration: 2000
        })
      },
      complete: function () {

      }
    })

  },
  loadSetting: function () {
    var that = this;
    wx.request({
      url: appData.apiurl + "/mobile/api/Unionstore/GetSetting",
      data: { "CustId": appData.custID },
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        var m = res.data.content.Information;
        var arra = new Array();
        if (m != null && m != "") {
          arra = m.split(",");
        }
        //从设置表读取需要显示的资料设置
        if (arra != null && arra.length > 0) {
          for (var i = 0; i < arra.length; i++) {
            var condition = arra[i];
            switch (condition) {
              case "1":
                that.setData({
                  hideUserName: false
                });
                break;
              case "2":
                that.setData({
                  hidePhone: false
                });
                break;
              case "3":
                that.setData({
                  hideShopName: false
                });
                break;
              case "4":
                that.setData({
                  hideIDCardDIV: false
                });
                break;
              case "5":
                that.setData({
                  hideBusinesslicenseDIV: false
                });
                break;

            }

          }
        }


      },
      fail: function () {

        wx.showToast({
          title: '获取设置失败',
          duration: 2000
        })
      },
      complete: function () {

      }
    });


  },
  loadApply: function () {

    var that = this;
    wx.request({
      url: appData.apiurl + "/mobile/api/Unionstore/GetApply",
      data: { "custID": appData.custID, "userID": wx.getStorageSync('UserInfo').UserID },
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        var m = res.data.content;
        if (m != null && m != "") {
          that.setData({
            UserName: m.UserName,
            Phone: m.Phone,
            ShopName: m.ShopName,
            IDCard: m.IDCard,
            hideIDCard: false,
            hideIDCardAdd: true,
            BusinessLicense: m.BusinessLicense,
            hideBusinesslicense: false,
            hideBusinesslicenseAdd: true,
            OrderNo: m.OrderNo
          });
          if (m.State == 1) {
            that.setData({
              popupWaitPayModel: false,
              ButtonDisabled: true,
              ButtonText: "您需支付保证金！"
            });
            that.Countdown();
          }
          if (m.State == 2) {
            that.setData({
              popupWaitModel: false,
              ButtonDisabled: true,
              ButtonText: "请耐心等待审核！"
            });

            that.Countdown();
          } else if (m.State == 3) {
            that.setData({
              popupPassedMode: false,
              ButtonDisabled: true,
              ButtonText: "已受理,不能修改资料.."
            });
            that.Countdown();
          } else if (m.State == 4) {
            that.setData({
              popupRefuseMode: false,
              ButtonText: "重新提交申请",
              CheckRemark: m.CheckRemark
            });
            that.Countdown();
            that.CheckPower();

           
          }


        }else{
          that.CheckPower();
        }

      },
      fail: function () {

        wx.showToast({
          title: '获取申请信息失败',
          duration: 2000
        })
      },
      complete: function () {

      }
    });

  },
  Countdown: function () {
    var that = this;
    var second = 60;
    setInterval(function () {
      second--;
      that.setData({
        second: second
      });
      if (second == 0) {
        wx.switchTab({
          url: '/pages/customIndex/index'
        });
      }
    }, 1000);
  },
  hideWaitPayModel: function () {
    var that = this;
    that.setData({
      popupWaitPayModel: true
    });
  },
  hideWaitModel: function () {
    var that = this;
    that.setData({
      popupWaitModel: true
    });
  },
  hidePassedMode: function () {
    var that = this;
    that.setData({
      popupPassedMode: true
    });
  },
  hideRefuseMode: function () {
    var that = this;
    that.setData({
      popupRefuseMode: true
    });

  },
  CheckPower: function () {
    var that = this;
    wx.request({
      url: appData.apiurl + "/mobile/api/Unionstore/CheckPower?custID=" + appData.custID + "&userID=" + wx.getStorageSync('UserInfo').UserID,
      data: {},
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        var response = res.data;
        if (response.code < 0) {
          that.setData({
            popupNoPowerMode: false,
            noPowerMsg: response.msg,
            ButtonText: response.content,
            ButtonDisabled: true
          });

          that.Countdown();

        }


      },
      fail: function () {

        wx.showToast({
          title: '检查权限失败',
          duration: 2000
        })
      },
      complete: function () {

      }
    })
  },
  bindUserNameInput: function (event) {
    var that = this;
    that.setData({
      UserName: event.detail.value
    })
  },
  bindPhoneInput: function (event) {
    var that = this;
    that.setData({
      Phone: event.detail.value
    })
  },
  bindShopNameInput: function (event) {
    var that = this;
    that.setData({
      ShopName: event.detail.value
    })
  },
  DoApply: function () {

    var that = this;
    var obj = new Object();
    if (!that.data.hideUserName && that.data.UserName == "") {

      wx.showModal({
        title: '温馨提示',
        content: '请填写姓名',
        showCancel: false
      })
      return;
    }
    if (!that.data.hidePhone && that.data.Phone == "") {

      wx.showModal({
        title: '温馨提示',
        content: '请填写电话',
        showCancel: false
      })
      return;
    }
    if (!that.data.hideShopName && that.data.ShopName == "") {

      wx.showModal({
        title: '温馨提示',
        content: '请填写店铺名称',
        showCancel: false
      })
      return;
    }

    if (!that.data.hideIDCardDIV && that.data.IDCard == "") {

      wx.showModal({
        title: '温馨提示',
        content: '请上传身份证照片',
        showCancel: false
      })
      return;
    }
    if (!that.data.hideBusinesslicenseDIV && that.data.BusinessLicense == "") {

      wx.showModal({
        title: '温馨提示',
        content: '请填写营业执照',
        showCancel: false
      })
      return;
    }

    obj.UserName = that.data.UserName;
    obj.Phone = that.data.Phone;
    obj.ShopName = that.data.ShopName;
    obj.UserId = wx.getStorageSync('UserInfo').UserID;
    obj.IDCard = that.data.IDCard;
    obj.BusinessLicense = that.data.BusinessLicense;;
    obj.CustID = appData.custID;
    wx.request({
      url: appData.apiurl + "/mobile/api/Unionstore/AddApply",
      data: obj,
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        var result = res.data.content;
        var needpay = result.NeedPay;
        if (needpay == 0) {
       
          wx.switchTab({
              url: '/pages/my/my'
            })
         
        } else {
        
            wx.navigateTo({
              url: '/pages/PayBond/PayBond?OrderId=' + result.OrderNo
            })
         

        }

      }
  ,
  fail: function () {

    wx.showToast({
      title: '检查权限失败',
      duration: 2000
    })
  },
  complete: function () {

  }
})  
   }
})